﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class TradePartnerBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private PubParasInfo pubParasInfo;
        private static readonly ITradePartnerDA tradePartnerDA = DataAccess.CreateTradePartnerDA();

        public TradePartnerBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TradePartnerBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> tradePartnerIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<TradePartnerInfo> tradePartnerInfos = new List<TradePartnerInfo>();
                TradePartnerInfo tradePartnerInfo = null;
                foreach (string id in tradePartnerIDS)
                {
                    tradePartnerInfo = tradePartnerDA.GetTradePartnerByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    tradePartnerInfos.Add(tradePartnerInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (tradePartnerDA.DeleteTradePartner(tradePartnerIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < tradePartnerIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TradePartnerInfo>(dataBase, tran, tradePartnerInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "OWNER", this.dataBaseID);
                        temp = temp + tradePartnerIDS[i] + ",";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (!string.IsNullOrEmpty(temp))
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return tradePartnerDA.GetAllTradePartner(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string tradePartnerID)
        {
            return tradePartnerDA.GetTradePartnerByID(tradePartnerID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            TradePartnerQueryEntity tradePartnerQuery = obj as TradePartnerQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(tradePartnerQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER.OWNER_ID " + tradePartnerQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(tradePartnerQuery.Description))
            {
                sqlWhere.Add(" OWNER.DESCRIPTION " + tradePartnerQuery.Description);
            }
            if (!string.IsNullOrEmpty(tradePartnerQuery.Company))
            {
                sqlWhere.Add(" OWNER.COMPANY " + tradePartnerQuery.Company);
            }
            if (!string.IsNullOrEmpty(tradePartnerQuery.City))
            {
                sqlWhere.Add(" OWNER.CITY " + tradePartnerQuery.City);
            }
            if (!string.IsNullOrEmpty(tradePartnerQuery.IsActive))
            {
                sqlWhere.Add(" OWNER.IS_ACTIVE " + tradePartnerQuery.IsActive);
            }
            sqlWhere.Add(" OWNER.TYPE = '" + tradePartnerQuery.Type + "' ");
            return tradePartnerDA.GetTradePartnerByQueryList(sqlWhere, tradePartnerQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string tradePartnerID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                TradePartnerInfo tradePartner = obj as TradePartnerInfo;
                if (ownerDA.CheckTradePartnerIDUnique(dataBase, tran, tradePartner.OwnerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2110_001", this.pubParasInfo.UserLanguage, new List<string> { tradePartner.OwnerID });
                }
                tradePartner.UpdatedBy = tradePartner.CreatedBy;
                tradePartner.CreatedDate = DateTime.Now;
                tradePartner.UpdatedDate = DateTime.Now;
                if (tradePartner.Type == "1")
                {
                    if (ownerDA.InsertTradePartner(dataBase, tran, tradePartner, this.dataBaseID) > 0)
                    {
                        new AuditDataBLL().InsertAuditData<TradePartnerInfo>(dataBase, tran, null, tradePartner, AuditAction.Insert, this.pubParasInfo.UserID, "OWNER", this.dataBaseID);
                        tradePartnerID = tradePartner.OwnerID;
                    }
                }
                else if (tradePartnerDA.InsertTradePartner(dataBase, tran, tradePartner, this.dataBaseID) > 0)
                {
                    tradePartnerID = tradePartner.OwnerID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return tradePartnerID;
        }

        public string OwnerImportByData(string[][] ImportByData, string IsUpdate)
        {
            Exception ex;
            string OwnerIDTrue = string.Empty;
            string OwnerIDFalse = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 1; ImportByData.Length > i; i++)
                {
                    string CS00004;
                    TradePartnerInfo tradePartnerInfo = new TradePartnerInfo();
                    for (int n = 0; ImportByData[i].Length > n; n++)
                    {
                        try
                        {
                            switch (ImportByData[0][n])
                            {
                                case "货主代码":
                                {
                                    if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                    {
                                        tradePartnerInfo.OwnerID = ImportByData[i][n];
                                    }
                                    continue;
                                }
                                case "货主名称":
                                {
                                    tradePartnerInfo.Company = ImportByData[i][n];
                                    continue;
                                }
                                case "描述":
                                {
                                    tradePartnerInfo.Description = ImportByData[i][n];
                                    continue;
                                }
                                case "增值税号":
                                {
                                    tradePartnerInfo.Vat = ImportByData[i][n];
                                    continue;
                                }
                                case "城市":
                                {
                                    tradePartnerInfo.City = ImportByData[i][n];
                                    continue;
                                }
                                case "省份":
                                {
                                    tradePartnerInfo.State = ImportByData[i][n];
                                    continue;
                                }
                                case "邮编":
                                {
                                    tradePartnerInfo.Zip = ImportByData[i][n];
                                    continue;
                                }
                                case "国家":
                                {
                                    tradePartnerInfo.Country = ImportByData[i][n];
                                    continue;
                                }
                                case "ISO国家码":
                                {
                                    tradePartnerInfo.CountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "地址行":
                                {
                                    tradePartnerInfo.Address = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1":
                                {
                                    tradePartnerInfo.Contact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话1":
                                {
                                    tradePartnerInfo.Phone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真1":
                                {
                                    tradePartnerInfo.Fax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1电子邮件":
                                {
                                    tradePartnerInfo.Email1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2":
                                {
                                    tradePartnerInfo.Contact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话2":
                                {
                                    tradePartnerInfo.Phone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真2":
                                {
                                    tradePartnerInfo.Fax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2电子邮件":
                                {
                                    tradePartnerInfo.Email2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B城市":
                                {
                                    tradePartnerInfo.BCity = ImportByData[i][n];
                                    continue;
                                }
                                case "B省份":
                                {
                                    tradePartnerInfo.BState = ImportByData[i][n];
                                    continue;
                                }
                                case "B邮编":
                                {
                                    tradePartnerInfo.BZip = ImportByData[i][n];
                                    continue;
                                }
                                case "B国家":
                                {
                                    tradePartnerInfo.BCountry = ImportByData[i][n];
                                    continue;
                                }
                                case "BISO国家码":
                                {
                                    tradePartnerInfo.BCountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单地址":
                                {
                                    tradePartnerInfo.BAddress = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人1":
                                {
                                    tradePartnerInfo.BContact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话1":
                                {
                                    tradePartnerInfo.BPhone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真1":
                                {
                                    tradePartnerInfo.BFax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件1":
                                {
                                    tradePartnerInfo.BEmail1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人2":
                                {
                                    tradePartnerInfo.BContact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话2":
                                {
                                    tradePartnerInfo.BPhone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真2":
                                {
                                    tradePartnerInfo.BFax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件2":
                                {
                                    tradePartnerInfo.BEmail2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段1":
                                {
                                    tradePartnerInfo.Udf1 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段2":
                                {
                                    tradePartnerInfo.Udf2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段3":
                                {
                                    tradePartnerInfo.Udf3 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段4":
                                {
                                    tradePartnerInfo.Udf4 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段5":
                                {
                                    tradePartnerInfo.Udf5 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段6":
                                {
                                    tradePartnerInfo.Udf6 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段7":
                                {
                                    tradePartnerInfo.Udf7 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段8":
                                {
                                    tradePartnerInfo.Udf8 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段9":
                                {
                                    tradePartnerInfo.Udf9 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段10":
                                {
                                    tradePartnerInfo.Udf10 = ImportByData[i][n];
                                    continue;
                                }
                                case "备注":
                                {
                                    tradePartnerInfo.Remark = ImportByData[i][n];
                                    continue;
                                }
                                case "创建人":
                                {
                                    tradePartnerInfo.CreatedBy = ImportByData[i][n];
                                    continue;
                                }
                            }
                        }
                        catch (Exception exception1)
                        {
                            ex = exception1;
                            if (!string.IsNullOrEmpty(tradePartnerInfo.OwnerID))
                            {
                                CS00004 = OwnerIDFalse;
                                OwnerIDFalse = CS00004 + tradePartnerInfo.OwnerID + PublicMethod.GetInstance().GetGlobalMsgResource("C003", this.pubParasInfo.UserLanguage) + ex.Message + "<br/>";
                            }
                        }
                    }
                    if (string.IsNullOrEmpty(tradePartnerInfo.IsRequiredExternalID))
                    {
                        tradePartnerInfo.IsRequiredExternalID = "N";
                    }
                    tradePartnerInfo.Finance_Logistical_Calc_By = "";
                    tradePartnerInfo.Finance_Logistical_Ctrl = "";
                    tradePartnerInfo.IsRequiredExternalID = "";
                    tradePartnerInfo.IsActive = "Y";
                    tradePartnerInfo.Type = "1";
                    tradePartnerInfo.UpdatedBy = tradePartnerInfo.CreatedBy;
                    if (!string.IsNullOrEmpty(tradePartnerInfo.OwnerID))
                    {
                        try
                        {
                            if (ownerDA.CheckTradePartnerIDUnique(tradePartnerInfo.OwnerID, this.dataBaseID[0]))
                            {
                                if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, tradePartnerInfo.OwnerID))
                                {
                                    OwnerIDFalse = OwnerIDFalse + tradePartnerInfo.OwnerID + PublicMethod.GetInstance().GetGlobalMsgResource("C008", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                                else if (IsUpdate == "Y")
                                {
                                    if (tradePartnerDA.UpdateTradePartner(dataBase, tran, tradePartnerInfo, this.dataBaseID) > 0)
                                    {
                                        OwnerIDFalse = OwnerIDFalse + tradePartnerInfo.OwnerID + PublicMethod.GetInstance().GetGlobalMsgResource("C004", this.pubParasInfo.UserLanguage) + "<br/>";
                                    }
                                }
                                else
                                {
                                    OwnerIDFalse = OwnerIDFalse + tradePartnerInfo.OwnerID + PublicMethod.GetInstance().GetGlobalMsgResource("C005", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                            }
                            else if (tradePartnerDA.InsertTradePartner(dataBase, tran, tradePartnerInfo, this.dataBaseID) > 0)
                            {
                                OwnerIDTrue = OwnerIDTrue + tradePartnerInfo.OwnerID + ";";
                            }
                        }
                        catch (Exception exception2)
                        {
                            ex = exception2;
                            if (!string.IsNullOrEmpty(tradePartnerInfo.OwnerID))
                            {
                                CS00004 = OwnerIDFalse;
                                OwnerIDFalse = CS00004 + tradePartnerInfo.OwnerID + PublicMethod.GetInstance().GetGlobalMsgResource("C006", this.pubParasInfo.UserLanguage) + ex.Message + "<br/>";
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception exception3)
            {
                ex = exception3;
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return OwnerIDFalse;
        }

        public string Update(object obj)
        {
            string tradePartnerID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                TradePartnerInfo tradePartner = obj as TradePartnerInfo;
                if (tradePartner.Type == "1")
                {
                    TradePartnerInfo oldTradePartnerInfo = ownerDA.GetTradePartnerByID(dataBase, tran, tradePartner.OwnerID, this.dataBaseID[0]);
                    if (ownerDA.UpdateTradePartner(dataBase, tran, tradePartner, this.dataBaseID) > 0)
                    {
                        new AuditDataBLL().InsertAuditData<TradePartnerInfo>(dataBase, tran, oldTradePartnerInfo, tradePartner, AuditAction.Update, this.pubParasInfo.UserID, "OWNER", this.dataBaseID);
                        tradePartnerID = tradePartner.OwnerID;
                    }
                }
                else if (tradePartnerDA.UpdateTradePartner(dataBase, tran, tradePartner, this.dataBaseID) > 0)
                {
                    tradePartnerID = tradePartner.OwnerID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return tradePartnerID;
        }
    }
}

